package cn.rkylin.oms.warehouse.local.localStockIn.controller;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.core.utils.StringUtil;
import cn.rkylin.oms.common.annotation.SystemControllerLog;
import cn.rkylin.oms.common.consts.StockInStatus;
import cn.rkylin.oms.common.context.CurrentUser;
import cn.rkylin.oms.common.context.WebContextFactory;
import cn.rkylin.oms.common.dto.DBLog;
import cn.rkylin.oms.common.utils.CodeUtil;
import cn.rkylin.oms.common.utils.LogUtil;
import cn.rkylin.oms.goods.service.GoodsServiceImpl;
import cn.rkylin.oms.goods.vo.PrjSkuVO;
import cn.rkylin.oms.warehouse.local.localStockIn.service.LocalStockInServiceImpl;
import cn.rkylin.oms.warehouse.stockin.dao.StockInDAOImpl;
import cn.rkylin.oms.warehouse.stockin.dao.StockInGoodsDAOImpl;
import cn.rkylin.oms.warehouse.stockin.domain.StockinType;
import cn.rkylin.oms.warehouse.stockin.vo.StockInGoodsVO;
import cn.rkylin.oms.warehouse.stockin.vo.StockInVO;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.*;

/**
 * 入库Controller
 *
 * @author 齐涛
 * @version 1.0
 * @created 11-9-2017 10:51:30
 */
@Controller
@RequestMapping("/localStockIn")
public class LocalStockInController extends ApolloController {
    private static final Log logger = LogFactory.getLog(LocalStockInController.class);

    private static final String PAGE_SELECT_STATEMENT = "pageSelectStockInLocal";
    private static final String PAGE_SUK_SELECT_STATEMENT = "selectStockInGoodsList";

    @Autowired
    private LocalStockInServiceImpl localStockInService;

    @Autowired
    private GoodsServiceImpl goodsServiceImpl;

    @Autowired
    private LogUtil logUtil;

    /**
     * 查询入库单列表
     *
     * @param quickSearch 快速查询条件
     * @param formJson    高级检索条件
     * @param start       第几页
     * @param length      每页多少行
     * @return 结果Map
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    @ResponseBody
    @RequestMapping(value = "/getStockInList")
    public Map<String, Object> getStockInList(String quickSearch, String formJson, String tabStockInStatus,
                                              @RequestParam(required = false, defaultValue = "0") int start, @RequestParam(required = false, defaultValue = "10") int length) {
        Map map = new HashMap();
        try {
            // 处理分页
            if (length == -1) {
                length = Integer.MAX_VALUE;
            }
            int page = start / length + 1;

            StockInVO param = new StockInVO();
            // 高级查询检索条件
            formJson = URLDecoder.decode(formJson, "UTF-8");
            param = JSONObject.parseObject(formJson, StockInVO.class);
            // 快速查询检索条件
            quickSearch = URLDecoder.decode(quickSearch, "UTF-8");
            if (StringUtils.isNotEmpty(quickSearch)) {
                param.setQuickSearch(quickSearch.trim());
            }
            param.setTabStockInStatus(tabStockInStatus);
            // 设置项目Id
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            String prjId = currentUser.getCurrentProject().getPrjId();
            param.setPrjId(prjId);

            // 处理转义的字段
            Map<String, String> replaceFieldsMap = new HashMap<String, String>();
            replaceFieldsMap.put("enableStatus", "enable");

            // 排序语句生成
            String orderStatement = getOrderString(StockInDAOImpl.class.getName(), PAGE_SELECT_STATEMENT, replaceFieldsMap);
            if (StringUtils.isNotEmpty(orderStatement)) {
                param.setOrderBy(orderStatement);
            }

            PageInfo<StockInVO> stockInList = localStockInService.findByWhereLocal(page, length, param);
            map.put(JSON_RESULT, SUCCESS);
            map.put(RECORDS_FILTERED, stockInList.getTotal());
            map.put(RECORDS_TOTAL, stockInList.getTotal());
            map.put(RETURN_DATA, stockInList.getList());

        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.getMessage(), ex);
            map.put(JSON_RESULT, FAILED);
            map.put(JSON_MSG, ex.getMessage());
        }

        return map;
    }

    /**
     * 查询入库单列表数量
     *
     * @param quickSearch      快速查询条件
     * @param formJson         高级检索条件
     * @param tabStockInStatus TAB入库状态
     * @param start            第几页
     * @param length           每页多少行
     * @return 结果Map
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    @ResponseBody
    @RequestMapping(value = "/getStockInListCount")
    public Map<String, Object> getStockInListCount(String quickSearch, String formJson, String tabStockInStatus,
                                                   @RequestParam(required = false, defaultValue = "0") int start, @RequestParam(required = false, defaultValue = "10") int length) {
        Map map = new HashMap();
        try {
//            Map waitAuditMap = this.getStockInList(quickSearch, formJson, StockInStatus.WAIT_AUDIT, start, length);
            Map waitStockInMap = this.getStockInList(quickSearch, formJson, StockInStatus.WAIT_STOCKIN, start, length);
//            Map stockIningMap = this.getStockInList(quickSearch, formJson, StockInStatus.STOCKINING, start, length);
            Map stockInedMap = this.getStockInList(quickSearch, formJson, StockInStatus.STOCKINED, start, length);

            map.put(JSON_RESULT, SUCCESS);
//            map.put("waitAuditCount", waitAuditMap.get(RECORDS_TOTAL));
            map.put("waitStockInCount", waitStockInMap.get(RECORDS_TOTAL));
//            map.put("stockIningCount", stockIningMap.get(RECORDS_TOTAL));
            map.put("stockInedCount", stockInedMap.get(RECORDS_TOTAL));

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            map.put(JSON_RESULT, FAILED);
            map.put(JSON_MSG, ex.getMessage());
        }

        return map;
    }

    /**
     * 查询入库单商品列表
     *
     * @param stockinId 入库单Id
     * @return 结果Map
     */
    @ResponseBody
    @RequestMapping(value = "/getStockInGoodsList")
    public Map<String, Object> getStockInGoodsList(String stockinId, String kind) {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        try {
            StockInGoodsVO stockInGoodsVO = new StockInGoodsVO();
            stockInGoodsVO.setStockinId(stockinId);
            stockInGoodsVO.setKind(kind);
            // 处理转义的字段
            Map<String, String> replaceFieldsMap = new HashMap<String, String>();
            // 排序语句生成
            String orderStatement = getOrderString(StockInGoodsDAOImpl.class.getName(), PAGE_SUK_SELECT_STATEMENT, replaceFieldsMap);
            if (StringUtils.isNotEmpty(orderStatement)) {
                stockInGoodsVO.setOrderBy(orderStatement);
            }

            List<StockInGoodsVO> stockInGoodsList = localStockInService.selectStockInGoodsList(stockInGoodsVO);

            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, stockInGoodsList);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }

        return returnMap;
    }

    /**
     * 审核入库单状态̬
     *
     * @param stockinId 入库单Id
     */
    @ResponseBody
    @RequestMapping(value = {"/auditStockIn"})
    @SystemControllerLog(module = "入库单管理", operation = "审核入库单状态", keyCode = "stockinId")
    public Map<String, Object> auditStockIn(String stockinId) {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        try {
            localStockInService.saveAuditStockIn(stockinId);

            logUtil.info(new DBLog(stockinId, "审核入库单", "入库单管理", "审核成功"));

            returnMap.put(JSON_RESULT, SUCCESS);
        } catch (Exception ex) {
            logUtil.error(new DBLog(stockinId, "审核入库单", "入库单管理", ex.getMessage()));
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("stockinId", stockinId);
        return returnMap;
    }

    /**
     * 删除查询入库单
     *
     * @param stockinId     入库单Id
     * @param stockInStatus 入库单状态
     */
    @ResponseBody
    @RequestMapping(value = {"/deleteStockIn"})
    @SystemControllerLog(module = "入库单管理", operation = "删除查询入库单", keyCode = "stockinId")
    public Map<String, Object> deleteStockIn(String stockinId, String stockInStatus) {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        try {
            localStockInService.deleteStockIn(stockinId, stockInStatus);

            logUtil.info(new DBLog(stockinId, "删除入库单", "入库单管理", "删除成功"));

            returnMap.put(JSON_RESULT, SUCCESS);
        } catch (Exception ex) {
            logUtil.error(new DBLog(stockinId, "删除入库单", "入库单管理", ex.getMessage()));
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("stockinId", stockinId);
        return returnMap;
    }

    /**
     * 添加入库单
     *
     * @param stockInVO 入库单VO
     */
    @SuppressWarnings("rawtypes")
    @ResponseBody
    @RequestMapping(value = "/insertStockIn")
    @SystemControllerLog(module = "入库单管理", operation = "添加入库单", keyCode = "stockinId")
    public Map<String, Object> insertStockIn(@RequestBody StockInVO stockInVO) {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        try {
            // 设置企业Id
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            Iterator iterator = currentUser.getEnterpriseMap().keySet().iterator();
            String entId = (String) iterator.next();
            stockInVO.setEntId(entId);

            // 设置项目Id
            String prjId = currentUser.getCurrentProject().getPrjId();
            stockInVO.setPrjId(prjId);

            // 设置入库Id
            String stockinId = UUID.randomUUID().toString().replace("-", "");
            stockInVO.setStockinId(stockinId);

            // 设置入库编码
            String prjShortName = currentUser.getCurrentProject().getPrjShortName();
            stockInVO.setStockinCode("SI" + prjShortName + CodeUtil.getCode());

            // 设置创建时间
            stockInVO.setCreateTime(new Date());

            // 设置入库商品信息
            List<StockInGoodsVO> list = stockInVO.getStockInGoodsVOList();
            for (int i = 0; i < list.size(); i++) {
                StockInGoodsVO stockInGoodsVO = (StockInGoodsVO) list.get(i);
                stockInGoodsVO.setStockinId(stockinId);
                String stockinSkuId = UUID.randomUUID().toString().replace("-", "");
                stockInGoodsVO.setStockinSkuId(stockinSkuId);
            }

            stockInVO.setStockinStatus("f");//本地入库单管理新增就是待入库
            // 添加入库单
            localStockInService.insertStockIn(stockInVO);

            logUtil.info(new DBLog(stockInVO.getStockinCode(), "添加入库单", "入库单管理", "添加成功"));

            returnMap.put(JSON_RESULT, SUCCESS);
        } catch (Exception ex) {
            logUtil.error(new DBLog(stockInVO.getStockinCode(), "添加入库单", "入库单管理", ex.getMessage()));
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("stockinId", stockInVO.getStockinId());
        return returnMap;
    }

    /**
     * 修改入库单(备注)
     *
     * @param stockInVO 入库单VO
     * @return 结果Map
     */
    @ResponseBody
    @RequestMapping(value = "/updateStockInRemark")
    @SystemControllerLog(module = "入库单管理", operation = "修改入库单(备注)", keyCode = "stockinId")
    public Map<String, Object> updateStockInRemark(@RequestBody StockInVO stockInVO) {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        try {
            localStockInService.updateStockInRemark(stockInVO);

            logUtil.info(new DBLog(stockInVO.getStockinCode(), "修改入库单备注", "入库单管理", "修改成功"));

            returnMap.put(JSON_RESULT, SUCCESS);
        } catch (Exception ex) {
            logUtil.error(new DBLog(stockInVO.getStockinCode(), "修改入库单备注", "入库单管理", ex.getMessage()));
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("stockinId", stockInVO.getStockinId());
        return returnMap;
    }

    /**
     * 修改入库单
     *
     * @param stockInVO 入库单VO
     */
    @ResponseBody
    @RequestMapping(value = "/updateStockIn")
    @SystemControllerLog(module = "入库单管理", operation = "修改入库单(备注)", keyCode = "stockinId")
    public Map<String, Object> updateStockIn(@RequestBody StockInVO stockInVO) {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        try {
            // 设置企业Id
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            Iterator iterator = currentUser.getEnterpriseMap().keySet().iterator();
            String entId = (String) iterator.next();
            stockInVO.setEntId(entId);

            // 设置项目Id
            String prjId = currentUser.getCurrentProject().getPrjId();
            stockInVO.setPrjId(prjId);

            // 设置入库商品信息
            List<StockInGoodsVO> list = stockInVO.getStockInGoodsVOList();
            for (int i = 0; i < list.size(); i++) {
                StockInGoodsVO stockInGoodsVO = (StockInGoodsVO) list.get(i);
                stockInGoodsVO.setStockinId(stockInVO.getStockinId());
                if (StringUtil.isEmpty(stockInGoodsVO.getStockinSkuId())) {
                    String stockinSkuId = UUID.randomUUID().toString().replace("-", "");
                    stockInGoodsVO.setStockinSkuId(stockinSkuId);
                }
            }

            // 修改入库单
            localStockInService.updateStockIn(stockInVO);

            logUtil.info(new DBLog(stockInVO.getStockinCode(), "修改入库单", "入库单管理", "修改成功"));

            returnMap.put(JSON_RESULT, SUCCESS);
        } catch (Exception ex) {
            logUtil.error(new DBLog(stockInVO.getStockinCode(), "修改入库单", "入库单管理", ex.getMessage()));
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("stockinId", stockInVO.getStockinId());
        return returnMap;
    }

    /**
     * 查询项目sku列表
     *
     * @return 结果Map
     * @throws Exception 异常
     */
    @ResponseBody
    @RequestMapping(value = "/getPrjSkuList")
    public Map<String, Object> getPrjSkuList(String formJson) {

        Map<String, Object> returnMap = new HashMap<String, Object>();

        try {
            formJson = URLDecoder.decode(formJson, "UTF-8");
            PrjSkuVO prjSkuVO = JSONObject.parseObject(formJson, PrjSkuVO.class);
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            String projectId = currentUser.getCurrentProject().getPrjId();
            prjSkuVO.setPrjId(projectId);

            List<PrjSkuVO> prjSkuVOList = goodsServiceImpl.selectPrjSkuListForStockIn(prjSkuVO);

            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, prjSkuVOList);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }

        return returnMap;
    }

    /**
     * 查询项目sku列表
     *
     * @return 结果Map
     * @throws Exception 异常
     */
    @ResponseBody
    @RequestMapping(value = "/getPrjSkuListForOrder")
    public Map<String, Object> getPrjSkuListForOrder(String formJson) {

        Map<String, Object> returnMap = new HashMap<String, Object>();

        try {
            formJson = URLDecoder.decode(formJson, "UTF-8");
            PrjSkuVO prjSkuVO = JSONObject.parseObject(formJson, PrjSkuVO.class);
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            String projectId = currentUser.getCurrentProject().getPrjId();
            prjSkuVO.setPrjId(projectId);
            if ("n".equals(prjSkuVO.getIsGift())) {
                prjSkuVO.setIsGift("w");
            }

            List<PrjSkuVO> prjSkuVOList = goodsServiceImpl.selectPrjSkuListForStockIn(prjSkuVO);

            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, prjSkuVOList);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }

        return returnMap;
    }
    
    /**
     * 查询项目sku列表
     *
     * @return 结果Map
     * @throws Exception 异常
     */
    @ResponseBody
    @RequestMapping(value = "/getPrjSkuListForOrder4ModifySku")
    public Map<String, Object> getPrjSkuListForOrder4ModifySku(String formJson) {
        
        Map<String, Object> returnMap = new HashMap<String, Object>();
        
        try {
            formJson = URLDecoder.decode(formJson, "UTF-8");
            PrjSkuVO prjSkuVO = JSONObject.parseObject(formJson, PrjSkuVO.class);
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            String projectId = currentUser.getCurrentProject().getPrjId();
            prjSkuVO.setPrjId(projectId);
            
            List<PrjSkuVO> prjSkuVOList = goodsServiceImpl.selectPrjSkuListForStockIn(prjSkuVO);
            
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, prjSkuVOList);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        
        return returnMap;
    }

    /**
     * 查询入库单信息
     *
     * @param stockinId 入库单Id
     * @return 结果Map
     * @throws Exception 异常
     */
    @ResponseBody
    @RequestMapping(value = {"/getStockInInfo/{stockinId}"})
    public Map<String, Object> getStockInInfo(@PathVariable String stockinId) throws Exception {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            StockInVO stockInVO = new StockInVO();
            stockInVO.setStockinId(stockinId);
            StockInVO data = localStockInService.getStockInInfo(stockInVO);

            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, data);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }

        return returnMap;
    }


    /**
     * 查询入库类型列表
     *
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = {"/getStockinTypeList"})
    public Map<String, Object> getStockinTypeList() throws Exception {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            List<StockinType> stockinTypeList = localStockInService.selectStockinTypeList();

            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, stockinTypeList);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }

        return returnMap;
    }

    @ResponseBody
    @RequestMapping(value = "/batchStockIn", method = RequestMethod.POST)
    @SystemControllerLog(module = "本地仓入库单管理", operation = "批量完成")
    public Map<String, Object> batchStockIn(@RequestBody List<StockInVO> stockinVOList) throws Exception {
        Map<String, Object> returnJSON = new HashMap<String, Object>();
        String stockInId = "";

        try {
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser == null) {
                returnJSON.put(JSON_RESULT, FAILED);
                returnJSON.put(JSON_MSG, "当前用户失效");
                return returnJSON;
            }

            int successCount = 0;
            int failCount = 0;
            for (int i = 0; i < stockinVOList.size(); i++) {
                stockInId = stockinVOList.get(i).getStockinId();
                String returnMsg = localStockInService.updateFinshStockIn(stockInId,currentUser.getFullName(),currentUser.getCurrentProject().getPrjId());
                if ("成功".equals(returnMsg)) {
                    successCount++;
                } else {
                    failCount++;
                }

                logUtil.info(new DBLog(stockInId, "批量完成入库单", "本地仓入库单管理", "批量完成成功"));
            }

            // 返回结果
            if (failCount == 0) {
                returnJSON.put(JSON_RESULT, SUCCESS);
                returnJSON.put(RETURN_DATA, "批量完成成功" + successCount + "条");
            } else {
                returnJSON.put(JSON_RESULT, FAILED);
                returnJSON.put(JSON_MSG, "批量完成成功" + successCount + "条，失败" + failCount + "条");
            }
        } catch (Exception ex) {
            logUtil.error(new DBLog(stockInId, "批量完成", "本地仓入库单管理", ex.getMessage()));
            logger.error(ex.getMessage(), ex);
            returnJSON.put(JSON_RESULT, FAILED);
            returnJSON.put(JSON_MSG, ex.getMessage());
        }

        return returnJSON;
    }

    @ResponseBody
    @RequestMapping(value = "/batchDelStockIn", method = RequestMethod.POST)
    @SystemControllerLog(module = "本地仓入库单管理", operation = "批量删除")
    public Map<String, Object> batchDelStockIn(@RequestBody List<StockInVO> stockinVOList) throws Exception {
        Map<String, Object> returnJSON = new HashMap<String, Object>();
        String stockInId = "";

        try {
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser == null) {
                returnJSON.put(JSON_RESULT, FAILED);
                returnJSON.put(JSON_MSG, "当前用户失效");
                return returnJSON;
            }

            int successCount = 0;
            int failCount = 0;
            for (int i = 0; i < stockinVOList.size(); i++) {
                stockInId = stockinVOList.get(i).getStockinId();
                String returnMsg = localStockInService.delStockIn(stockInId,currentUser.getFullName());
                if ("成功".equals(returnMsg)) {
                    successCount++;
                } else {
                    failCount++;
                }

                logUtil.info(new DBLog(stockInId, "批量删除入库单", "本地仓入库单管理", "批量删除成功"));
            }

            // 返回结果
            if (failCount == 0) {
                returnJSON.put(JSON_RESULT, SUCCESS);
                returnJSON.put(RETURN_DATA, "批量删除成功" + successCount + "条");
            } else {
                returnJSON.put(JSON_RESULT, FAILED);
                returnJSON.put(JSON_MSG, "批量删除成功" + successCount + "条，失败" + failCount + "条");
            }
        } catch (Exception ex) {
            logUtil.error(new DBLog(stockInId, "批量删除", "本地仓入库单管理", ex.getMessage()));
            logger.error(ex.getMessage(), ex);
            returnJSON.put(JSON_RESULT, FAILED);
            returnJSON.put(JSON_MSG, ex.getMessage());
        }

        return returnJSON;
    }

}
